@echo off
:: Script to download and install CLion on Windows if not already installed

:: Check if running as administrator
net session >nul 2>&1
if %errorLevel% == 0 (
    echo Running as administrator...
) else (
    echo Please run this script as administrator!
    pause
    exit /b
)

:: Set the default download URL for CLion Windows version
set CLION_URL=https://download.jetbrains.com/cpp/CLion-2024.3.2.exe

:: Set the default CLion installation directory (default is C:\Program Files\JetBrains\CLion)
set INSTALL_DIR=C:\Program Files\JetBrains\CLion

:: Check if CLion is already installed by verifying the existence of the installation directory
if exist "%INSTALL_DIR%" (
    echo CLion is already installed at %INSTALL_DIR%. Skipping download and installation.
    pause
    exit /b
)

:: Set the path to save the installer (Windows 11 default download directory)
set DOWNLOAD_PATH=%USERPROFILE%\Downloads\CLion-Installer.exe

:: Download the CLion installer using PowerShell
echo CLion is not installed. Downloading CLion installer...
powershell -Command "Invoke-WebRequest -Uri '%CLION_URL%' -OutFile '%DOWNLOAD_PATH%'"

:: Check if the download was successful
if not exist "%DOWNLOAD_PATH%" (
    echo Failed to download CLion installer!
    pause
    exit /b
)

:: Install CLion
echo Installing CLion...
start /wait "" "%DOWNLOAD_PATH%"

:: Check if the installation was successful
if not exist "%INSTALL_DIR%" (
    echo CLion installation failed!
    pause
    exit /b
)


:: Clean up the installer
echo Cleaning up the installer...
del "%DOWNLOAD_PATH%"

echo CLion installation completed successfully!
pause